#"w"：写入模式（覆盖原有内容）。
#"a"：追加模式（在文件末尾添加内容）。
# "r"：读取模式（默认）。
file = None
try:
    with open("test.txt", "w", encoding="utf-8") as file:
        file.write("Hello, Python!\n这是第二行")
    with open("test.txt", "a", encoding="utf-8") as file:
        file.write("\n这是第三行")
    with open("test.txt", "r", encoding="utf-8") as file:
        content = file.read()
        print(content)
    # 按行读取
    with open("test.txt", "r", encoding="utf-8") as file:
        lines = file.readlines()
        for line in lines:
            print("行内容：", line.strip())
except:
    print("文件不存在")
finally:
    if file != None:
        file.close()
